///
/// {
//           "rp": {
//             "clientID": "19100717375019793291301001",
//             // 处方类别ID
//             "rpTypeID": 0,
//             "rpTypeName": "西药",
//             "clientType": 0,
//             "rpSource": 2,
//             "patientID": "_patient.personID",
//             "patientName": "_patient.patientName",
//             "providerType": 0,
//             // 药品供应商ID
//             "providerID": "",
//             // 药品供应商名称
//             "providerName": "",
//             "doctorOrgType": 2,
//             "doctorOrgID": "doctor.orgID",
//             "sectionCode": "this.userInfo.doctorInfo.sectionCode",
//             "section": "this.userInfo.doctorInfo.sectionName",
//             "doctorID": "this.userInfo.keyID",
//             "doctorName": "this.userInfo.doctorName",
//             // 初步诊断
//             "primaryDiagnosis": "this.inquiryResult",
//             "primaryDiagnosisNo": "this.primaryDiagnosisNo",
//             // 总剂
//             "totalDoses": 0,
//             // 医嘱
//             "enjoin": "",
//             // 费别
//             "feeType": 1,
//             // 处方总价
//             "recipeAllPrice": 0.0,
//             "rpStatus": "1",
//             "addUser": "this.userInfo.doctorName",
//             "chineseMedicine": {
//               "frequence": 0,
//               // 用法
//               "direction": "",
//               // 每日剂量
//               "everyDose": 0.0
//             }
//           },
//           "patientInfo": {
//             "patientName": "_patient.patientName",
//             "patientSex": "_patient.patientSex",
//             "patientAge": "_patient.patientAge",
//             "crowd": "",
//             "patientPhone": "_patient.patientPhone",
//             "patientIdNo":"_patient.patientIdNo",
//             "birthDay": "_patient.birthDay",
//             // 监护人姓名
//             "guardianName": "",
//             "guardianIdcardNum": "",
//             // 病情描述
//             "illDesc": "",
//             // 过敏史
//             "historyOfAllergy": "this.sickness.ALLERGY",
//             // 过敏史详情
//             "allergyDetail": "this.secondValue.ALLERGY.join()",
//             // 既往病史
//             "historyOfSickness": "this.sickness.ILLNESS",
//             // 既往病史详情
//             "sicknessDetail": "this.secondValue.ILLNESS.join()",
//             // 肝功能
//             "liver": "this.sickness.LIVER",
//             // 肾功能
//             "renal": "this.sickness.KIDNEY",
//             // 妊娠哺乳
//             "pregnancy": "this.sickness.PREGNANCY",
//             // 辨病
//             "diseaseDiff": "",
//             // 辨证
//             "syndromeDiff": "",
//             // 患者地址
//             "address": "this.addressData + _patient.detailAddress",
//             // 患者经度
//             "longitude": "longitude",
//             // 患者纬度
//             "latitude": "latitude"
//           },
//           "rpMedicines": []
//         }
///
///
import 'package:doctor_prescribe_lib/western_medicine/medical_search/entity/medical_search_entity.dart'
    as MedicineSearchBean;

class OfflineRpEntity {
  Rp rp;
  PatientInfo patientInfo;
  List<MedicineSearchBean.Datas> rpMedicines;

  OfflineRpEntity({this.rp, this.patientInfo, this.rpMedicines = const []});

  OfflineRpEntity.fromJson(dynamic json) {
    rp = json["rp"] != null ? Rp.fromJson(json["rp"]) : null;
    patientInfo = json["patientInfo"] != null
        ? PatientInfo.fromJson(json["patientInfo"])
        : null;
    if (json["rpMedicines"] != null) {
      rpMedicines = [];
      json["rpMedicines"].forEach((v) {
        rpMedicines.add(MedicineSearchBean.Datas.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    if (rp != null) {
      map["rp"] = rp.toJson();
    }
    if (patientInfo != null) {
      map["patientInfo"] = patientInfo.toJson();
    }
    if (rpMedicines != null) {
      map["rpMedicines"] = rpMedicines.map((v) => v.toJson()).toList();
    }
    return map;
  }
}

class PatientInfo {
  String patientName;
  int patientSex;
  int patientAge;
  String crowd;
  String patientPhone;
  String patientIdNo;
  String birthDay;
  String guardianName;
  String guardianIdcardNum;
  String illDesc;
  String historyOfAllergy;
  String allergyDetail;
  String historyOfSickness;
  String sicknessDetail;
  String liver;
  String renal;
  String pregnancy;
  String diseaseDiff;
  String syndromeDiff;
  String address;
  String longitude;
  String latitude;

  PatientInfo(
      {this.patientName,
      this.patientSex,
      this.patientAge,
      this.crowd,
      this.patientPhone,
      this.patientIdNo,
      this.birthDay,
      this.guardianName,
      this.guardianIdcardNum,
      this.illDesc,
      this.historyOfAllergy,
      this.allergyDetail,
      this.historyOfSickness,
      this.sicknessDetail,
      this.liver,
      this.renal,
      this.pregnancy,
      this.diseaseDiff,
      this.syndromeDiff,
      this.address,
      this.longitude,
      this.latitude});

  PatientInfo.fromJson(dynamic json) {
    patientName = json["patientName"];
    patientSex = json["patientSex"];
    patientAge = json["patientAge"];
    crowd = json["crowd"];
    patientPhone = json["patientPhone"];
    patientIdNo = json["patientIdNo"];
    birthDay = json["birthDay"];
    guardianName = json["guardianName"];
    guardianIdcardNum = json["guardianIdcardNum"];
    illDesc = json["illDesc"];
    historyOfAllergy = json["historyOfAllergy"];
    allergyDetail = json["allergyDetail"];
    historyOfSickness = json["historyOfSickness"];
    sicknessDetail = json["sicknessDetail"];
    liver = json["liver"];
    renal = json["renal"];
    pregnancy = json["pregnancy"];
    diseaseDiff = json["diseaseDiff"];
    syndromeDiff = json["syndromeDiff"];
    address = json["address"];
    longitude = json["longitude"];
    latitude = json["latitude"];
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["patientName"] = patientName;
    map["patientSex"] = patientSex;
    map["patientAge"] = patientAge;
    map["crowd"] = crowd;
    map["patientPhone"] = patientPhone;
    map["patientIdNo"] = patientIdNo;
    map["birthDay"] = birthDay;
    map["guardianName"] = guardianName;
    map["guardianIdcardNum"] = guardianIdcardNum;
    map["illDesc"] = illDesc;
    map["historyOfAllergy"] = historyOfAllergy;
    map["allergyDetail"] = allergyDetail;
    map["historyOfSickness"] = historyOfSickness;
    map["sicknessDetail"] = sicknessDetail;
    map["liver"] = liver;
    map["renal"] = renal;
    map["pregnancy"] = pregnancy;
    map["diseaseDiff"] = diseaseDiff;
    map["syndromeDiff"] = syndromeDiff;
    map["address"] = address;
    map["longitude"] = longitude;
    map["latitude"] = latitude;
    return map;
  }
}

class Rp {
  String clientID;
  int rpTypeID;
  String rpTypeName;
  int clientType;
  int rpSource;
  String patientID;
  String patientName;
  int orgTypeID = 1;

  int _orgType = 0;

  String get orgType => _orgType == 0 ? doctorOrgID : _orgType;

  set orgType(val) {
    orgType = val;
  }

  /// 保存处方时的机构id
  String _providerID = '';

  String get providerID => _providerID.isEmpty ? doctorOrgID : _providerID;

  set providerID(val) {
    _providerID = val;
  }

  /// 保存处方时的机构类型
  int _providerType = 0;

  int get providerType => _providerType == 0 ? orgTypeID : _providerType;

  set providerType(val) {
    _providerType = val;
  }

  String providerName;
  int doctorOrgType;
  String doctorOrgID;
  int medicineFormID;
  String sectionCode;
  String section;
  String doctorID;
  String doctorName;
  String primaryDiagnosis;
  String primaryDiagnosisNo;
  int totalDoses;
  String enjoin;
  int feeType;
  double recipeAllPrice;
  String rpStatus;
  String addUser;
  ChineseMedicine chineseMedicine;

  Rp(
      {this.clientID,
      this.medicineFormID,
      this.rpTypeID,
      this.rpTypeName,
      this.clientType,
      this.rpSource,
      this.patientID,
      this.patientName,
      this.providerName,
      this.doctorOrgType,
      this.doctorOrgID,
      this.sectionCode,
      this.section,
      this.doctorID,
      this.doctorName,
      this.primaryDiagnosis,
      this.primaryDiagnosisNo,
      this.totalDoses,
      this.enjoin,
      this.feeType,
      this.recipeAllPrice,
      this.rpStatus,
      this.addUser,
      this.chineseMedicine});

  Rp.fromJson(dynamic json) {
    medicineFormID = json["medicineFormID"];
    clientID = json["clientID"];
    rpTypeID = json["rpTypeID"];
    rpTypeName = json["rpTypeName"];
    clientType = json["clientType"];
    rpSource = json["rpSource"];
    patientID = json["patientID"];
    patientName = json["patientName"];
    providerName = json["providerName"];
    doctorOrgType = json["doctorOrgType"];
    doctorOrgID = json["doctorOrgID"];
    sectionCode = json["sectionCode"];
    section = json["section"];
    doctorID = json["doctorID"];
    doctorName = json["doctorName"];
    primaryDiagnosis = json["primaryDiagnosis"];
    primaryDiagnosisNo = json["primaryDiagnosisNo"];
    totalDoses = json["totalDoses"];
    enjoin = json["enjoin"];
    feeType = json["feeType"];
    recipeAllPrice = json["recipeAllPrice"];
    rpStatus = json["rpStatus"];
    addUser = json["addUser"];
    chineseMedicine = json["chineseMedicine"] != null
        ? ChineseMedicine.fromJson(json["chineseMedicine"])
        : null;
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["medicineFormID"] = medicineFormID;
    map["clientID"] = clientID;
    map["rpTypeID"] = rpTypeID;
    map["rpTypeName"] = rpTypeName;
    map["clientType"] = clientType;
    map["rpSource"] = rpSource;
    map["patientID"] = patientID;
    map["patientName"] = patientName;
    map["providerType"] = providerType;
    map["providerID"] = providerID;
    map["providerName"] = providerName;
    map["doctorOrgType"] = doctorOrgType;
    map["doctorOrgID"] = doctorOrgID;
    map["sectionCode"] = sectionCode;
    map["section"] = section;
    map["doctorID"] = doctorID;
    map["doctorName"] = doctorName;
    map["primaryDiagnosis"] = primaryDiagnosis;
    map["primaryDiagnosisNo"] = primaryDiagnosisNo;
    map["totalDoses"] = totalDoses;
    map["enjoin"] = enjoin;
    map["feeType"] = feeType;
    map["recipeAllPrice"] = recipeAllPrice;
    map["rpStatus"] = rpStatus;
    map["addUser"] = addUser;
    if (chineseMedicine != null) {
      map["chineseMedicine"] = chineseMedicine.toJson();
    }
    return map;
  }
}

class ChineseMedicine {
  int frequence;
  String direction;
  int everyDose;

  ChineseMedicine({this.frequence, this.direction, this.everyDose});

  ChineseMedicine.fromJson(dynamic json) {
    frequence = json["frequence"];
    direction = json["direction"];
    everyDose = json["everyDose"];
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["frequence"] = frequence;
    map["direction"] = direction;
    map["everyDose"] = everyDose;
    return map;
  }
}

class RpMedicine {
  //             medicineID: i.medicineID,
  //             goodsID: i.goodsID,
  //             medicineClassID: 0,
  //             medicineClassName: "西药",
  //             medicineName: i.medicineName,
  //             specification: i.specification,
  //             medicineAmount: i.medicineAmount, // 用量： 多少g!
  //             unit: i.unit, // 单位
  //             unitPrice: i.unitPrice,
  //             formType: i.formType,
  //             takeDose: i.takeDose, // 服用剂量
  //             takeFrequence: i.takeFrequence, // 服用频次
  //             takeDirection: i.takeDirection, // 服用方法
  //             medicationDays: i.medicationDays, // 用药天数
  //             memo: i.memo // 备注
  String medicineID;
  String goodsID;
  int medicineClassID;
  String medicineClassName;
  String medicineName;
  String specification;
  String medicineAmount;
  String unit;
  String unitPrice;
  String formType;
  String takeDose;
  String takeFrequence;
  String takeDirection;
  String medicationDays;
  String memo;

  RpMedicine(
      {this.medicineID,
      this.goodsID,
      this.medicineClassID,
      this.medicineClassName,
      this.medicineName,
      this.specification,
      this.medicineAmount,
      this.unit,
      this.unitPrice,
      this.formType,
      this.takeDose,
      this.takeFrequence,
      this.takeDirection,
      this.medicationDays,
      this.memo});

  RpMedicine.fromJson(dynamic json) {
    medicineID = json["medicineID"];
    goodsID = json["goodsID"];
    medicineClassID = json["medicineClassID"];
    medicineClassName = json["medicineClassName"];
    medicineName = json["medicineName"];
    specification = json["specification"];
    medicineAmount = json["medicineAmount"];
    unit = json["unit"];
    unitPrice = json["unitPrice"];
    formType = json["formType"];
    takeDose = json["takeDose"];
    takeFrequence = json["takeFrequence"];
    takeDirection = json["takeDirection"];
    medicationDays = json["medicationDays"];
    memo = json["memo"];
  }

  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map["medicineID"] = medicineID;
    map["goodsID"] = goodsID;
    map["medicineClassID"] = medicineClassID;
    map["medicineClassName"] = medicineClassName;
    map["medicineName"] = medicineName;
    map["specification"] = specification;
    map["medicineAmount"] = medicineAmount;
    map["unit"] = unit;
    map["unitPrice"] = unitPrice;
    map["formType"] = formType;
    map["takeDose"] = takeDose;
    map["takeFrequence"] = takeFrequence;
    map["takeDirection"] = takeDirection;
    map["medicationDays"] = medicationDays;
    map["memo"] = memo;
    return map;
  }
}
